package driver.ui.driver;

import java.util.ArrayList;

import driver.ui.R;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.view.View;

public class Addnewcontact extends Activity{
	 @Override
	    public void onCreate(Bundle savedInstanceState) {		 
		 	
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.addcontact);

	        
	 }
	 public void addcontactClicked(View view)
	 {
	      // add mobile phone number
   	  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
   	  int rawContactInsertIndex = ops.size();

   	   ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   	                          .withValue(RawContacts.ACCOUNT_TYPE, null)
   	                          .withValue(RawContacts.ACCOUNT_NAME,null )
   	                          .build());
   	  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   	                          .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
   	                          .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
   	                          .withValue(Phone.NUMBER, "1-412-000-0000")
   	                          .build());
   	  ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
   	                          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
   	                          .withValue(Data.MIMETYPE,
   	                           StructuredName.CONTENT_ITEM_TYPE)
   	                          .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
   	                          .build());  
   	  try {
			ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OperationApplicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 	Intent addIntent=new Intent();
	 	addIntent.setAction("Contactlist");
	 	startActivity(addIntent);

		 
	 }

}
